#!/usr/bin/env bash

# ----------------------------------------------------------------------
# Filename	:  02-defPolicy-feature.sh
# Version	:  1.0
# Date		:  2020/06/01
# Author	:  Lz
# Email		:  lz843723683@gmail.com
# History	:     
#                  Version 1.0, 2020/06/01
# Function	:  测试三权分立 - 默认策略
# Out		:        
#              0 => TPASS
#              1 => TFAIL
#              other=> TCONF
# ----------------------------------------------------------------------

# 测试主题
Title_Env_LTFLIB="三权分立(系统特性) - 默认策略"

HeadFile_Source_LTFLIB="${LIB_SSHAUTO}"

testuser1_defPolicy="ltfdefPolicy"
passwd1_defPolicy="olleH717.12.#$"
userip_defPolicy="localhost"
AddUserNames_LTFLIB="${testuser1_defPolicy}"
AddUserPasswds_LTFLIB="${passwd1_defPolicy}"


## TODO : 个性化,初始化
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
TestInit_LTFLIB(){
        # 配置免密登录
        SshAuto_OneConfig_LTFLIB "${userip_defPolicy}" "${testuser1_defPolicy}" "${passwd1_defPolicy}"
        TestRetParse_LTFLIB "配置免密登录" "True" "no" "yes"

	SshAuto_SetIpUser_LTFLIB "${userip_defPolicy}" "${testuser1_defPolicy}"
        TestRetParse_LTFLIB "设置默认IP和用户名" "True" "no" "yes"

	return ${TPASS}
}


## TODO : 清理函数
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
TestClean_LTFLIB(){
	return ${TPASS}
}


## TODO ：sysadm 可以重启 sshd 服务
testcase_1(){
	local svrname_defPolicy="sshd"

	SshAuto_CmdLocalSys_LTFLIB "sudo systemctl restart $svrname_defPolicy" "no" "no"
        TestRetParse_LTFLIB "sysadm 可以正常重启 $svrname_defPolicy 服务" "False"

	systemctl status $svrname_defPolicy
        TestRetParse_LTFLIB "$svrname_defPolicy 服务正常运行" "False"
}


## TODO ：普通用户 不可以重启 sshd 服务
testcase_2(){
	local svrname_defPolicy="sshd"

	SshAuto_CmdDef_LTFLIB "sudo systemctl restart $svrname_defPolicy" "no" "yes"
        TestRetParse_LTFLIB "普通用户 ${testuser1_defPolicy} 可以不能重启 $svrname_defPolicy 服务" "False"
}


## TODO ：新建普通用户，可以对文件进行相关操作
testcase_3(){
        local tmpfile_defPolicy="${TmpTestDir_LTFLIB}/ltf-defpolicy-sopft"

	# 新建文件
	SshAuto_CmdDef_LTFLIB "touch $tmpfile_defPolicy" "no" "no"
	if [ -f "$tmpfile_defPolicy" ];then
		# 新建成功
		OutputRet_LTFLIB $TPASS
        	TestRetParse_LTFLIB "普通用户新建文件 $tmpfile_defPolicy" "False"
	else
		OutputRet_LTFLIB $TFAIL
        	TestRetParse_LTFLIB "普通用户新建文件 $tmpfile_defPolicy" "False"
	fi

	# 写文件
	local flag="Hello Kylin~"
	SshAuto_CmdDef_LTFLIB "echo \"$flag\" > $tmpfile_defPolicy" "no" "no"
	cat ${tmpfile_defPolicy} | grep "$flag" 
	if [ $? -eq 0 ];then
		OutputRet_LTFLIB $TPASS
        	TestRetParse_LTFLIB "普通用户写文件 $tmpfile_defPolicy" "False"
	else
		cat ${tmpfile_defPolicy}
		OutputRet_LTFLIB $TFAIL
        	TestRetParse_LTFLIB "普通用户写文件 $tmpfile_defPolicy" "False"
	fi

	# 修改权限
	local power="rwx"
	SshAuto_CmdDef_LTFLIB "chmod a+$power $tmpfile_defPolicy" "no" "no"
	ls -al $tmpfile_defPolicy | grep "${power}${power}${power}"
	if [ $? -eq 0 ];then
		OutputRet_LTFLIB $TPASS
        	TestRetParse_LTFLIB "普通用户修改文件 $tmpfile_defPolicy 权限" "False"
	else
		ls -al $tmpfile_defPolicy
		OutputRet_LTFLIB $TFAIL
        	TestRetParse_LTFLIB "普通用户修改文件 $tmpfile_defPolicy 权限" "False"
	fi

	# 修改系统文件/etc/rc.local文件权限
	power="rwx"
	local rcfile="/etc/rc.local"
	SshAuto_CmdDef_LTFLIB "chmod a+$power $rcfile" "no" "yes"
        TestRetParse_LTFLIB "普通用户修改文件 $rcfile 权限" "False"

	if [ -f "$tmpfile_defPolicy" ];then
		SshAuto_CmdDef_LTFLIB "rm -rf $tmpfile_defPolicy" "yes" "no"
	fi
}


## TODO : 测试用例集
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
Testsuite_LTFLIB(){
	testcase_1
	testcase_2
	testcase_3

	return $TPASS
}


#----------------------------------------------#

source "${LIB_LTFLIB}"
Main_LTFLIB $@
